﻿using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;

namespace Neo4jConnect.Rest
{
    public class HttpClientRelationshipService : IRelationshipService
    {
        private readonly HttpClient client;

        public HttpClientRelationshipService(HttpClient client)
        {
            this.client = client;
        }
        
        public string Create(ulong nodeId, string relationshipData)
        {            
            return client
                .Post(string.Format("node/{0}/relationships", nodeId), new StringContent(relationshipData))            
                .EnsureStatusCode(HttpStatusCode.Created)
                .Content.ReadAsString();                       
        }
        
        public void SetProperties(ulong nodeId, string relationshipData)
        {
            var requestUri = string.Format("relationship/{0}/properties", nodeId);
            client
                .Put(requestUri, new StringContent(relationshipData, null, "application/json"))
                .EnsureStatusCode(HttpStatusCode.NoContent);            
        }
        
        public string GetProperties(ulong nodeId)
        {            
            return client
                .Get(string.Format("relationship/{0}/properties", nodeId))
                .EnsureStatusCode(HttpStatusCode.OK, HttpStatusCode.NoContent)
                .Content.ReadAsString();                                   
        }
        
        public void RemoveProperties(ulong relationshipId)
        {            
            client
                .Delete(string.Format("relationship/{0}/properties", relationshipId))
                .EnsureStatusCode(HttpStatusCode.NoContent);            
        }
        
        public void SetProperty(ulong relationshipId, string propertyName, string propertyValue)
        {
            var requestUri = string.Format("relationship/{0}/properties/{1}", relationshipId, propertyName);
            client
                .Put(requestUri, new StringContent(@"""" + propertyValue + @""""))
                .EnsureStatusCode(HttpStatusCode.NoContent);            
        }
        
        public string GetProperty(ulong relationshipId, string propertyName)
        {            
            return client
                .Get(string.Format("relationship/{0}/properties/{1}", relationshipId, propertyName))
                .EnsureStatusCode(HttpStatusCode.OK)
                .Content.ReadAsString();                                               
        }
        
        public void RemoveProperty(ulong relationshipId, string propertyName)
        {            
            client
                .Delete(string.Format("relationship/{0}/properties/{1}", relationshipId, propertyName))
                .EnsureStatusCode(HttpStatusCode.NoContent);            
        }
        
        public void Delete(ulong relationshipId)
        {            
            client
                .Delete(string.Format("relationship/{0}", relationshipId))
                .EnsureStatusCode(HttpStatusCode.NoContent);            
        }
        
        public string GetRelationshipsOnNode(ulong nodeId, RelationshipDirection relationshipDirection, IEnumerable<string> relationshipTypes)
        {
            string requestUri = string.Empty;

            if (relationshipTypes.Count() > 0)
            {
                string types = relationshipTypes.Aggregate((rt1, rt2) => rt1 + "&" + rt2);
                requestUri = string.Format("node/{0}/relationships/{1}/{2}", nodeId, relationshipDirection.ToString().ToLower(), types);
            }
            else
            {
                requestUri = string.Format("node/{0}/relationships/{1}", nodeId, relationshipDirection.ToString().ToLower());
            }

            return client
                .Get(requestUri)
                .EnsureStatusCode(HttpStatusCode.OK)
                .Content.ReadAsString();            
        }
    }
}
